﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MaxLeaf.DB;
using MaxLeaf.Common;
using System.Data.SqlClient;


namespace MaxLeaf.DAL.W2K
{
    /// <summary>
    /// 分发基类,add/update/delete
    /// </summary>
    public   class BaseDAL : IDALAction
    {

         
        public enum GetDataType
        {
            OrgData

        }
        //维护表名
        protected string strK3TableName = null;
        protected string strWebTableName = null;
        //对应mkeymap使用的key
        protected string strK3MapKeyFieldName = "FitemID";
        protected string strWebMapKeyFieldName = string.Empty;
        protected string strWebMapKeyTableName = string.Empty;
        //主key是独立key,还是和其他字段一起组成key
        protected bool blnMapKeyUseOtherKeyFieldsCond = false;
        //辅助key，就是多字段组成一个key
        protected Dictionary<string, object> dictOtherKeyFieldsCond = null;
        //原始数据和目标数据的辅助key值不一样时用
        protected Dictionary<string, object> dictDestOtherKeyFieldsCond = null;
        //实际数据库key
        protected string strK3DestRealKeyFieldName = string.Empty;
        protected string strWebOrgDestRealKeyFieldName=string.Empty;
        //维护的字段，不包括key
        protected string strFieldNames = null;
        //新增时是否填写key值
        protected bool mblnNeedEditKeyValue = true;
        //mapkey是不是自增长的
        protected bool mblnIsIdentity = false;

        protected string strFItemClassID = null;

        /// <summary>
        /// mapkey
        /// </summary>
        /// <param name="dbOrgWebReadCon"></param>
        /// <param name="dbDestK3Con"></param>
        /// <param name="strOrgKeyValue"></param>
        /// <returns></returns>
        public virtual string GetDestKeyValue(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, string strKeyValue)
        {
            string strSql = "select K3KeyValue from mWebK3KeyMap  where TableName=@TableName and WebKeyValue=@WebKeyValue";
            DataRow dr = dbDestK3Con.GetDataFirstRow(strSql, new SqlParameter[]{
                new SqlParameter("@TableName",strWebMapKeyTableName)
                ,new SqlParameter("@WebKeyValue",strKeyValue)
            });

            
            if(dr==null)
            {
                return string.Empty;
            }
            return CvtFunc.ToString(dr[0]);
        }

        /// <summary>
        /// 根据mapkey删除表数据
        /// 如果通过key找不到，表示不是通过程序分发的，忽略
        /// </summary>
        /// <param name="dbOrgWebReadCon"></param>
        /// <param name="dbDestK3Con"></param>
        /// <param name="strKeyValue"></param>
        public virtual void Delete(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, string strKeyValue)
        {

            //string strDestKeyValue = GetDestKeyValue(dbOrgWebCon,   dbDestK3Con,   strKeyValue);
            //if (strDestKeyValue.Length == 0)
            //{
            //    return ;
            //}

            //Dictionary<string, object> dictCond = new Dictionary<string, object>() { { strK3MapKeyFieldName, strDestKeyValue } };
            //if (blnMapKeyUseOtherKeyFieldsCond && dictOtherKeyFieldsCond != null)
            //{
            //    foreach (KeyValuePair<string, object> tmp in dictOtherKeyFieldsCond)
            //    {
            //        dictCond.Add(tmp.Key, tmp.Value);
            //    }
            //}
            //KeyValuePair<string, IDataParameter[]> kvp = dbDestK3Con.CreateCondParam(dictCond);
            //string strSql = string.Format(@"delete from {0} where  {1}", strK3TableName, kvp.Key);
            //dbDestK3Con.ExecuteSql(strSql, kvp.Value);
            

             
            //    //删除mapkey
            //    KeyValuePair<string, IDataParameter[]> kvpParam = dbDestK3Con.CreateCondParam(new Dictionary<string, object>() { { strK3MapKeyFieldName, strDestKeyValue } });
            //    dbDestK3Con.ExecuteSql(string.Format("delete from {0} where {1}",strMapKeyTableName, kvpParam.Key), kvpParam.Value);
             


        }


        /// <summary>
        /// Add没有单独处理,放在update
        /// 每次全部update
        /// </summary>
        /// <param name="dbOrgWebReadCon"></param>
        /// <param name="dbDestK3Con"></param>
        /// <param name="strKeyValue"></param>
        public virtual int Add(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, string strKeyValue)
        {


            return Update(dbOrgWebReadCon,dbOrgWebWriteCon, dbDestK3Con, strKeyValue);

        }
        //判断原来有没有mapkey,已分发过
        const string TMPDESTKEYVALUE = "-10000";
        string strDestKeyValue = TMPDESTKEYVALUE;

        /// <summary>
        /// 每次全部update
        /// </summary>
        /// <param name="dbOrgWebReadCon">来源数据库</param>
        /// <param name="dbDestK3Con">目标数据库</param>
        /// <param name="strKeyValue"></param>
        public virtual int Update(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, string strKeyValue)
        {
            KeyValuePair<string, IDataParameter[]> kvpParam;
            string strWebOrgKeyValue = strKeyValue;
            string strUpdateFieldName = strFieldNames;

            #region 根据主帐套FitemID取得分账套FitemID

            strDestKeyValue = GetDestKeyValue(dbOrgWebReadCon, dbOrgWebWriteCon, dbDestK3Con, strKeyValue);
            if (strDestKeyValue.Length==0)
            {
                strDestKeyValue = TMPDESTKEYVALUE;
            }
            #endregion 根据主帐套FitemID取得分账套FitemID

            kvpParam = CreateKeyParaCond(dbDestK3Con, strK3MapKeyFieldName, strDestKeyValue, true);

            DataTable dtData = dbDestK3Con.GetDataTable(
                string.Format("select {0} from {1} where {2}"
                        , strK3MapKeyFieldName + "," + strUpdateFieldName
                        , strK3TableName
                        , kvpParam.Key
                        )
                , kvpParam.Value
                );
            DataRow drData = null;
            LogStatus logStatus = LogStatus.Update;

            DataTable dtOrgData = DAL_GetData(dbOrgWebReadCon, dbOrgWebWriteCon, dbDestK3Con, (int)GetDataType.OrgData, strWebOrgKeyValue);
            if (dtOrgData == null || dtOrgData.Rows.Count == 0)
            {
                return 0;
            }
            DataRow drOrgData = dtOrgData.Rows[0];


            //如果用keymap找不到,就用第2key(比如fnumber)再对应一次
            if (dtData.Rows.Count == 0 && !string.IsNullOrEmpty(strK3DestRealKeyFieldName))
            {
                kvpParam = CreateKeyParaCond(dbDestK3Con, strK3DestRealKeyFieldName,CvtFunc.ToString( drOrgData[strWebOrgDestRealKeyFieldName]), true);

                dtData = dbDestK3Con.GetDataTable(
                    string.Format("select {0} from {1} where {2}"
                            , strK3MapKeyFieldName + "," + strUpdateFieldName
                            , strK3TableName
                            , kvpParam.Key
                            )
                    , kvpParam.Value
                    );
            }
            if (dtData.Rows.Count == 0)
            {
                //add
                strDestKeyValue = TMPDESTKEYVALUE;

                logStatus = LogStatus.Add;
                drData = dtData.NewRow();
                if (mblnNeedEditKeyValue)
                {
                     DAL_GetFieldData(dbOrgWebReadCon, dbOrgWebWriteCon,dbDestK3Con, strK3MapKeyFieldName, drOrgData,drData );

                    if ((strUpdateFieldName + ",").IndexOf(strK3MapKeyFieldName + ",", StringComparison.InvariantCultureIgnoreCase) < 0)
                    {
                        strUpdateFieldName = strK3MapKeyFieldName + "," + strUpdateFieldName;
                    }
                }


                if (dictDestOtherKeyFieldsCond != null)
                {
                    foreach (KeyValuePair<string, object> tmp in dictDestOtherKeyFieldsCond)
                    {
                        drData[tmp.Key] = tmp.Value;
                        if ((strUpdateFieldName + ",").IndexOf(tmp.Key + ",", StringComparison.InvariantCultureIgnoreCase) < 0)
                        {
                            strUpdateFieldName = tmp.Key + "," + strUpdateFieldName;
                        }
                    }
                }
                else if (dictOtherKeyFieldsCond != null)
                {
                    foreach (KeyValuePair<string, object> tmp in dictOtherKeyFieldsCond)
                    {
                        drData[tmp.Key] = tmp.Value;
                        if ((strUpdateFieldName + ",").IndexOf(tmp.Key + ",", StringComparison.InvariantCultureIgnoreCase) < 0)
                        {
                            strUpdateFieldName = tmp.Key + "," + strUpdateFieldName;
                        }
                    }
                }
            }
            else
            {
                drData = dtData.Rows[0];
            }

            //其他字段填值
            string[] arrFieldName = strUpdateFieldName.Split(',');
            foreach (string strField in arrFieldName)
            {
                  DAL_GetFieldData(dbOrgWebReadCon, dbOrgWebWriteCon,dbDestK3Con, strField, drOrgData, drData);
            }

            Update_Before(dbOrgWebReadCon, dbOrgWebWriteCon, dbDestK3Con, drOrgData, drData);

            //add or update
            KeyValuePair<string, IDataParameter[]> kvp = dbDestK3Con.CreateParamSql(strUpdateFieldName, strK3TableName, drData, logStatus, strK3MapKeyFieldName);
            string strIdentity = dbDestK3Con.GetValue(kvp.Key, kvp.Value);
            if (mblnIsIdentity && logStatus == LogStatus.Add)
            {
                drData[strK3MapKeyFieldName] = strIdentity;
            }
            if (strDestKeyValue.Equals(TMPDESTKEYVALUE))
            {

                //得到mapkey,保存
                if (!string.IsNullOrEmpty(strK3DestRealKeyFieldName))
                {
                    kvpParam = CreateKeyParaCond(dbDestK3Con, strK3DestRealKeyFieldName, CvtFunc.ToString(drData[strK3DestRealKeyFieldName]), true);

                    dtData = dbDestK3Con.GetDataTable(
                        string.Format("select {0} from {1} where {2}"
                                , strK3MapKeyFieldName
                                , strK3TableName
                                , kvpParam.Key
                                )
                        , kvpParam.Value
                        );
                    strDestKeyValue = CvtFunc.ToString(dtData.Rows[0][strK3MapKeyFieldName]);
                }
                else
                {
                    strDestKeyValue = CvtFunc.ToString(drData[strK3MapKeyFieldName]);
                }
                SaveKeyMap(dbOrgWebReadCon, dbOrgWebWriteCon, dbDestK3Con, strWebOrgKeyValue, strDestKeyValue);

            }

            if (dbDestK3Con.ServerMasterDB)
            {
                if(CvtFunc.ToInt(drData[strK3MapKeyFieldName])==0)
                {
                    WriteK3Log(dbDestK3Con, CvtFunc.ToInt(strDestKeyValue));
                }
                else
                {
                    WriteK3Log(dbDestK3Con, CvtFunc.ToInt(drData[strK3MapKeyFieldName]));
                }
            }


            UpdateOtherData(dbOrgWebReadCon, dbOrgWebWriteCon, dbDestK3Con, drOrgData, drData);

            return CvtFunc.ToInt(drData[strK3MapKeyFieldName]);

        }

        protected virtual void WriteK3Log(DBAccess dbDestK3Con, int intKeyValue)
        {
            
        }

        protected virtual void Update_Before(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, DataRow drOrgData, DataRow drData)
        {
            Update_t_Item(dbDestK3Con, CvtFunc.ToString(drData["FitemID"]), CvtFunc.ToString(drData["FNumber"]), CvtFunc.ToString(drData["Fname"]));
        }

        protected virtual void SaveKeyMap(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, string strOrgKeyValue, string strDestKeyValue)
        {
            WebKeyMapDict map = new WebKeyMapDict
            {
                TableName = strWebMapKeyTableName
                ,
                WebMapKeyFieldName = strWebMapKeyFieldName
                ,
                K3MapKeyFieldName =(strWebMapKeyTableName.Equals("k3_goods_barcode", StringComparison.InvariantCultureIgnoreCase )? "fitemid":strK3MapKeyFieldName)
                ,
                WebKeyValue = strOrgKeyValue
                ,
                K3KeyValue = strDestKeyValue
            };
            
            SaveK3WebKeyMap(dbDestK3Con, map);

            if (map.TableName.Equals("mItemGroupKeyMap_1", StringComparison.InvariantCultureIgnoreCase) || map.TableName.Equals("mItemGroupKeyMap_2", StringComparison.InvariantCultureIgnoreCase))
            {
            }
            else
            {
                dbOrgWebWriteCon.WebKeyMap.Add(map);
            }
        }

        private void SaveK3WebKeyMap(DBAccess dbDestK3Con, WebKeyMapDict map)
        {
            string strSql = @"if exists(select * from  mWebK3KeyMap where TableName=@TableName and WebKeyValue=@WebKeyValue)
                begin
                    update mWebK3KeyMap  set  K3KeyValue=@K3KeyValue  where TableName=@TableName and WebKeyValue=@WebKeyValue and not K3KeyValue=@K3KeyValue
                end
            else
                begin
                    insert into mWebK3KeyMap(TableName,WebKeyValue,K3KeyValue) values(@TableName,@WebKeyValue,@K3KeyValue)
                end
            ";
            dbDestK3Con.ExecuteSql(strSql, new SqlParameter[] { 
                new SqlParameter("@TableName",map.TableName)
                ,new SqlParameter("@WebKeyValue",map.WebKeyValue)
                ,new SqlParameter("@K3KeyValue",map.K3KeyValue)
            });
        }

        

        protected string GetMaxFNumber(DBAccess dbDestK3Con, string strFParentNumber, int iLen)
        {
            string strSql = string.Format("select max(FShortNumber) from t_item where  {0} and FItemClassID=" + strFItemClassID
                , (strFParentNumber.Length == 0 ? "flevel=1" : "flevel=" + (strFParentNumber .Split('.').Length+1)+ " and left(FNumber," + (strFParentNumber.Length + 1) + ")=@FParentNumber")
                );
            int iMax = CvtFunc.ToInt(dbDestK3Con.GetValue(strSql
                    , dbDestK3Con.CreateCondParam(new Dictionary<string, object>() { { "FParentNumber", strFParentNumber + "." } }).Value
                )) + 1;
            while (dbDestK3Con.GetDataFirstRow(string.Format("select FNumber from t_item where FShortNumber=@FNumber and {0} and FItemClassID=" + strFItemClassID
                                    , (strFParentNumber.Length == 0 ? "flevel=1" : "flevel=" + (strFParentNumber.Split('.').Length + 1) + "  and left(FNumber," + (strFParentNumber.Length + 1) + ")=@FParentNumber")
                        )
                , dbDestK3Con.CreateCondParam(new Dictionary<string, object>() { { "FParentNumber", strFParentNumber + "." }, { "FNumber", BaseFunctions.FormatWithZero(iMax.ToString(), iLen) } }).Value) != null)
            {
                iMax++;
            }

            return BaseFunctions.FormatWithZero(iMax.ToString(), iLen);
        }


        /// <summary>
        /// 更新完主数据，再更新其他数据
        /// </summary>
        /// <param name="dbOrgWebReadCon"></param>
        /// <param name="dbDestK3Con"></param>
        /// <param name="drOrgData"></param>
        /// <param name="drData"></param>
        protected virtual void UpdateOtherData(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, DataRow drOrgData, DataRow drData)
        {

        }

        /// <summary>
        /// 根据主key+辅助key建条件
        /// </summary>
        /// <param name="dbDestK3Con"></param>
        /// <param name="strKeyFieldName"></param>
        /// <param name="strDestKeyValue"></param>
        /// <param name="blnDest"></param>
        /// <returns></returns>
        private KeyValuePair<string, IDataParameter[]> CreateKeyParaCond(DBAccess dbDestK3Con, string strKeyFieldName, string strDestKeyValue, bool blnDest)
        {
            Dictionary<string, object> dictCond = new Dictionary<string, object>() { { strKeyFieldName, strDestKeyValue } };
            if (!blnMapKeyUseOtherKeyFieldsCond && strKeyFieldName.Equals(strK3MapKeyFieldName, StringComparison.InvariantCultureIgnoreCase))
            {
            }
            else
            {
                if (blnDest && dictDestOtherKeyFieldsCond != null)
                {
                    foreach (KeyValuePair<string, object> tmp in dictDestOtherKeyFieldsCond)
                    {
                        dictCond.Add(tmp.Key, tmp.Value);
                    }
                }
                else if (dictOtherKeyFieldsCond != null)
                {
                    foreach (KeyValuePair<string, object> tmp in dictOtherKeyFieldsCond)
                    {
                        dictCond.Add(tmp.Key, tmp.Value);
                    }
                }
            }
            KeyValuePair<string, IDataParameter[]> kvp = dbDestK3Con.CreateCondParam(dictCond);
            return kvp;
        }

        /// <summary>
        /// 根据FielName赋值
        /// </summary>
        /// <param name="dbOrgWebReadCon"></param>
        /// <param name="dbDestK3Con"></param>
        /// <param name="strFieldName"></param>
        /// <param name="drOrgData"></param>
        /// <param name="drDestData"></param>
        protected virtual void DAL_GetFieldData(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, string strK3FieldName, DataRow drOrgData, DataRow drDestData)
        {
            if (strK3FieldName.Equals(strK3MapKeyFieldName ,StringComparison.InvariantCultureIgnoreCase))
            {
                drDestData[strK3FieldName] = drOrgData[strWebMapKeyFieldName];
            }
            else
            {
                drDestData[strK3FieldName] = drOrgData[strK3FieldName];
            }
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="dbOrgWebReadCon"></param>
        /// <param name="dbDestK3Con"></param>
        /// <param name="intdataType"></param>
        /// <param name="objParam"></param>
        /// <returns></returns>
        protected virtual DataTable DAL_GetData(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, int intdataType, object objParam)
        {

            GetDataType dataType = (GetDataType)intdataType;
            if (dataType == GetDataType.OrgData)
            {
                return GetOrgData(dbOrgWebReadCon, dbOrgWebWriteCon, CvtFunc.ToString(objParam));
            }


            throw new MaxLeafException(string.Format("GetData {0} 没注册", intdataType));

        }

        /// <summary>
        /// 获取来源表数据
        /// </summary>
        /// <param name="dbOrgWebReadCon"></param>
        /// <param name="strOrgKeyValue"></param>
        /// <returns></returns>
        private DataTable GetOrgData(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, string strWebKeyValue)
        {

            KeyValuePair<string, IDataParameter[]> kvpParam = CreateKeyParaCond(dbOrgWebReadCon, strWebMapKeyFieldName, strWebKeyValue, false);

            DataTable dtOrgData = dbOrgWebReadCon.GetDataTable(
             string.Format("select {0} from {1} where {2}"
                     , "*"
                     , strWebTableName
                     , kvpParam.Key
                     )
             , kvpParam.Value
             );


            return dtOrgData;
        }

       
       
        /// <summary>
        /// 取得mapkey,没有找到就先强制分发一次
        /// </summary>
        /// <param name="dbOrgWebReadCon"></param>
        /// <param name="dbDestK3Con"></param>
        /// <param name="strOrgKeyValue"></param>
        /// <returns></returns>
        public virtual string GetDestMapKeyValue(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, string strOrgKeyValue)
        {

            string strSql = "select K3KeyValue from mWebK3KeyMap  where TableName=@TableName and WebKeyValue=@WebKeyValue";
            string strDestMapKeyValue = dbDestK3Con.GetValue(strSql, new SqlParameter[]{
                new SqlParameter("@TableName",strWebMapKeyTableName)
                ,new SqlParameter("@WebKeyValue",strOrgKeyValue)
            });
            if (strDestMapKeyValue.Length > 0)
            {
                return strDestMapKeyValue;
            }

            return CvtFunc.ToString( Update(dbOrgWebReadCon,dbOrgWebWriteCon, dbDestK3Con, strOrgKeyValue));

           
        }


        /// <summary>
        /// 取得mapkey,没有找到就先强制分发一次
        /// </summary>
        /// <param name="dbOrgWebReadCon"></param>
        /// <param name="dbDestK3Con"></param>
        /// <param name="strOrgKeyValue"></param>
        /// <returns></returns>
        public virtual string GetOrgMapKeyValue(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, string strDestKeyValue)
        {
            string strSql = "select WebKeyValue  from mWebK3KeyMap  where TableName=@TableName and K3KeyValue=@K3KeyValue";
            return  dbDestK3Con.GetValue(strSql, new SqlParameter[]{
                new SqlParameter("@TableName",strWebMapKeyTableName)
                ,new SqlParameter("@K3KeyValue",strDestKeyValue)
            });
             

        }


        protected string GetShortNumber(string strFNumber)
        {
            string[] arr = strFNumber.Split('.');
            return arr[arr.Length - 1];
        }

        protected virtual void Update_t_Item(DBAccess dbDestK3Con, string strFItemID, string strFnumber, string strFName)
        {
            string strSql = string.Empty;
            KeyValuePair<string, IDataParameter[]> kvp = new KeyValuePair<string, IDataParameter[]>();

            if (strFItemID.Length == 0) return;
            if (CvtFunc.ToInt(strFItemID) <= 0) throw new MaxLeafException(string.Format("FItemID is {0}", strFItemID));

            string strOldFNumber = dbDestK3Con.GetValue("select fnumber from t_Item Where  FitemID=@FitemID  AND FItemClassID = @FItemClassID", new SqlParameter[] { new SqlParameter("@FitemID", strFItemID), new SqlParameter("@FItemClassID", strFItemClassID) });

            strSql = "Update t_Item Set   Fparentid=@Fparentid,Fnumber=@Fnumber,FName=@FName,FShortNumber=@FShortNumber,FFullNumber=@Fnumber Where  FitemID=@FitemID";
            kvp = dbDestK3Con.CreateCondParam(new Dictionary<string, object>() { { "Fparentid", GetFParentId(dbDestK3Con, strFnumber) }, { "Fnumber", strFnumber }, { "FName", strFName }, { "FShortNumber", GetShortNumber(strFnumber) }, { "FitemID", strFItemID } });
            dbDestK3Con.ExecuteSql(strSql, kvp.Value);

            if (!strOldFNumber.Equals(strFnumber, StringComparison.InvariantCultureIgnoreCase))
            {
                if (!strOldFNumber.Split('.').Length.Equals(strFnumber.Split('.').Length))
                {
                    throw new MaxLeafException(string.Format("Cannot change fnumber {0} to {1}", strOldFNumber, strFnumber));
                }

                strSql = "Update t_Item Set FNumber = {fn INSERT(FNumber, 1, @Len, @FNumber)},FFullNumber = {fn INSERT(FNumber, 1, @Len, @FNumber)} Where FNumber Like @OldFNumber AND FItemClassID = @FItemClassID";
                kvp = dbDestK3Con.CreateCondParam(new Dictionary<string, object>() { { "Len", strFnumber.Length }, { "Fnumber", strFnumber }, { "OldFNumber", strOldFNumber + ".%" }, { "FItemClassID", strFItemClassID } });
                dbDestK3Con.ExecuteSql(strSql, kvp.Value);

                if (!(strK3TableName == null || strK3TableName.Length == 0))
                {
                    strSql = string.Format("UPDATE {0} Set FNumber =i.F1  From ( Select FNumber F1,FItemID FID1,FParentID FPID1 From t_Item Where FNumber Like @FNumber AND FItemClassID =  @FItemClassID ) i Where FItemID=i.FID1 "
                        ,(strK3TableName.Equals("t_ICItem", StringComparison.InvariantCultureIgnoreCase )?"t_ICItemCore":strK3TableName)
                        );
                    kvp = dbDestK3Con.CreateCondParam(new Dictionary<string, object>() { { "FNumber", strFnumber + ".%" }, { "FItemClassID", strFItemClassID } });
                    dbDestK3Con.ExecuteSql(strSql, kvp.Value);
                }
            }
            //Update t_Item Set FNumber = {fn INSERT(FNumber, 1, @P1, @P2)} Where FNumber Like @P3 AND FItemClassID = 4',N'@P1 int,@P2 varchar(80),@P3 varchar(80)',5,'00.01','00.00.%'

            //'UPDATE t_ICItemCore Set FNumber =i.F1  From ( Select FNumber F1,FItemID FID1,FParentID FPID1 From t_Item Where FNumber Like @P1 AND FItemClassID = 4 ) i Where FItemID=i.FID1 ',N'@P1 varchar(80)','00.01.%'
        }

        protected string GetFParentId(DBAccess dbDestK3Con, string strFnumber)
        {
            string[] arrFnumber = strFnumber.Split('.');            
            string strParentFNumber = string.Join(".", arrFnumber, 0, arrFnumber.Length - 1);
            if (strParentFNumber.Length == 0)
            {
                return "0";
            }

            return dbDestK3Con.GetValue("select FitemID from t_item where fnumber=@fnumber and FItemClassID=@FItemClassID", new SqlParameter[] { new SqlParameter("@fnumber", strParentFNumber), new SqlParameter("@FItemClassID", strFItemClassID) });
        }
    }
}
